package ncku.account.data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Calendar;

public abstract class AbstractTransaction {
	private static volatile long currentId = 0;
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
	
	private long id;  //The index of transactions
	protected Calendar calendar = Calendar.getInstance();  //交易日期
	protected String description;  //The description of this transaction
	protected int amount;  //The money of this transaction(交易金額)
	protected Category category;  //交易分類
	private ArrayList<SubTransaction> subTransactions;  //子細項
	
	//指定transaction id
	private static synchronized void setId(AbstractTransaction transaction) {
		if (currentId==Long.MAX_VALUE) throw new RuntimeException("Transaction counter has an overflow"); //$NON-NLS-1$
		transaction.id = currentId++;
	}
	
	//建構一筆新的交易
	public AbstractTransaction(Date date, String description, int amount, Category category, 
							   ArrayList<SubTransaction> subTransactions) {
		super();
		if ((category==null) || (description==null))
			throw new IllegalArgumentException();
		this.id = currentId;
		this.calendar.setTime(date);
		this.description = description;
		this.amount = amount;
		this.category = category;
		this.subTransactions = subTransactions;
		setId(this);
	}
	
	
	//讀取交易細目
	public String getDescription(){
		return this.description;
	}
	
	//讀取sub Transaction
	public ArrayList<SubTransaction> getSubTransactionForExport(){
		return this.subTransactions;
	}
	//讀取sub Transaction個數
	public int getSubTransactionNumber(){
		return this.subTransactions.size();
	}
	
	//讀取sub Transaction中的某一項
	public SubTransaction getSubTrans(int index){
		return this.subTransactions.get(index);
	}
	
	//回傳整個sub Transaction
	public ArrayList<SubTransaction> getAllSubTrans(){
		return this.subTransactions;
	}
	
	//讀取交易金額
	public int getAmount(){
		return this.amount;
	}
	
	//讀取交易金額(String)
	public String getStringAmount(){
		return Integer.toString(this.amount);
	}

	//讀取交易目錄
	public String getCategory(){
		return this.category.getName();
	}
	
	//讀取交易日期,輸出模式為yyyy-MM-dd (ex.2012/11/30)
	public String getDate(){
		return sdf.format(this.calendar.getTime());
	}
	
	public Date getDateinForm(){
		return this.calendar.getTime();
	}
}
